﻿Ext.define('HIT.controller.InvoiceBuyController', {
	extend : 'Ext.app.Controller',
	models : [ 'DealInvoiceBuy' ],
	stores : [ 'DealInvoiceBuy' ],
	views : [ 'DealInvoiceBuyGrid', 'DealInvoiceBuyWin'],

	onButtonClick : function(button, e, options) {

		// 发票购入
		if (button.id == 'deal-invoice-buy') {
			p = Share.createPanelByAlias('widget.dealinvoicebuywin');
			Share.showPanelAsWindow(p);
			Ext.getCmp('functionId').setValue('1');
		} 
		// 编辑
		else if (button.id == 'deal-invoicebuy-edit') {
			var grid = Ext.getCmp('tab-deal-invoicebuy');
			if (grid == undefined) {
				return;
			}
			var selModel = grid.getSelectionModel();
			if (selModel.hasSelection()) {
				if (selModel.getCount() != 1) {
					Share.ShowMessage('I0009','修改');
					return;
				} else {
					var model = selModel.getSelection()[0];
					var invoiceFlag = model.get('invoiceFlag');
					if (invoiceFlag != 0) {
						Share.ShowMessage('E0069');
						return;
					}
					var docentry = model.get('invoiceBuyId');
					Share.AjaxRequest({
						url : ctx + '/getInvoiceDataForEdit',
						params : {
							docentry : docentry
						},
						callback : function(json) {
							if (json.count == '1') {
								// 打开编辑窗口
								p = Share.createPanelByAlias('widget.dealinvoicebuywin');
								Share.showPanelAsWindow(p);
								Ext.getCmp('functionId').setValue('-1');
								
								Ext.getCmp('invoiceBuyId').setValue(docentry);
								Ext.getCmp('invoiceBuyCode').setValue(json.code);
								Ext.getCmp('invoiceBuySno').setValue(json.sno);
								Ext.getCmp('invoiceBuyEno').setValue(json.eno);
								Ext.getCmp('invoiceBuyType').setValue(json.type);
								Ext.getCmp('invoiceBuyDate').setValue(new Date(json.date));
								Ext.getCmp('invoiceMach').setValue(json.mach);
								Ext.getCmp('invoiceBuyRemark').setValue(json.remark);
							} else {
								Share.ShowMessage('E0070');
								Ext.getCmp('tab-deal-invoicebuy').getStore().reload();
							}
						}
					});
				}
			} else {
				Share.ShowMessage('I0008');
				return;
			}
		} 
		// 删除
		else if (button.id == 'deal-invoicebuy-delete') {
			var grid = Ext.getCmp('tab-deal-invoicebuy');
			if (grid == undefined) {
				return;
			}
			var selModel = grid.getSelectionModel();
			if (selModel.hasSelection()) {
				var model = selModel.getSelection();
				for(var j = 0; j < model.length; j++){
					var invoiceFlag = model[j].get('invoiceFlag');
					if (invoiceFlag != 0) {
						Share.ShowMessage('E0069');
						return;
					}
                }
				Ext.MessageBox.confirm(Share.T('提示'),Share.T('是否确认删除？'),function(btn){
					if(btn == 'yes'){
						var docentry = [];
	                	for(var i = 0; i < model.length; i++){
	                		docentry.push(model[i].get('invoiceBuyId'));
	                    }
						Share.AjaxRequest({
							url : ctx + '/deleteInvoiceList',
							params : {
								docentry : docentry.join(",")
							},
							callback : function(json) {
								if (json.resultcode == '0') {
									Share.ShowMessage('I0006','删除');
									Ext.getCmp('invoiceSdate').setValue('');
									Ext.getCmp('invoiceEdate').setValue('');
									var store = Ext.getCmp('tab-deal-invoicebuy').getStore();
									store.currentPage = 1;
									store.load();
								} else {
									Share.ShowMessage('I0007','删除');
								}
							}
						});
					}else{
						return;
					}
				});
			}else{
				Share.ShowMessage('I0008');
				return;
			}
		}
		// 查询
		else if (button.id == 'deal-invoicebuy-search') {
			if (!Ext.getCmp('invoiceSdate').isValid() || !Ext.getCmp('invoiceEdate').isValid()) {
				return;
			}
			var invoiceBuyType = Ext.getCmp('invoiceBuyGridType').getValue();
			if (invoiceBuyType == 0) {
				invoiceBuyType = '';
			}
			var buysdate = Ext.getCmp('invoiceSdate').getValue();
			var buyedate = Ext.getCmp('invoiceEdate').getValue();
			var store = Ext.getCmp('tab-deal-invoicebuy').getStore();
			store.currentPage = 1;
      		store.removeAll();
            store.on('beforeload', function(store) {
     			Ext.apply(store.proxy.extraParams, {
     				invoiceType : invoiceBuyType,
     				buysdate : buysdate,
     				buyedate : buyedate
     			});
            });
            store.load();
		}
	},
	
	save : function(win) {
		if (!Ext.getCmp('invoice_buy_form').getForm().isValid()) {
			return;
		}
		var functionId = Ext.getCmp('functionId').getValue();
		var invoiceBuyId = Ext.getCmp('invoiceBuyId').getValue();
		
		var invoiceBuyCode = Ext.getCmp('invoiceBuyCode').getValue();
		var invoiceBuySno = Ext.getCmp('invoiceBuySno').getValue();
		var invoiceBuyEno = Ext.getCmp('invoiceBuyEno').getValue();
		if (invoiceBuySno > invoiceBuyEno) {
			Ext.getCmp('invoiceBuyEno').markInvalid(Share.T('起始号不能大于结束号。'));
			Ext.getCmp('invoiceBuySno').markInvalid(Share.T('起始号不能大于结束号。'));
			return;
		}
		if (invoiceBuyEno - invoiceBuySno > 10000) {
			Share.ShowMessage('I0079');
			return;
		}
		var invoiceBuyType = Ext.getCmp('invoiceBuyType').getValue();
		var invoiceBuyDate = Ext.getCmp('invoiceBuyDate').getValue();
		var invoiceMach = Ext.getCmp('invoiceMach').getValue();
		var invoiceBuyRemark = Ext.getCmp('invoiceBuyRemark').getValue();
		// 新建保存
		if (functionId == 1) {
			Ext.Ajax.request({
                url     : ctx + '/saveInvoiceBuy',
                params  : {
					invoiceBuyId : invoiceBuyId,
					invoiceBuyCode : invoiceBuyCode,
					invoiceBuySno : invoiceBuySno,
					invoiceBuyEno : invoiceBuyEno,
					invoiceBuyType : invoiceBuyType,
					invoiceBuyDate : invoiceBuyDate,
					invoiceMach : invoiceMach,
					invoiceBuyRemark : invoiceBuyRemark
				},
                success : function(response, options) {
                    var json = Ext.decode(response.responseText);
					if (json.resultcode == '0') {
						Share.ShowMessage('I0006','保存');
						win.close();
						Ext.getCmp('invoiceSdate').setValue('');
						Ext.getCmp('invoiceEdate').setValue('');
						Ext.getCmp('invoiceBuyGridType').setValue('');
						Ext.getCmp('tab-deal-invoicebuy').getStore().reload();
					} else {
						win.close();
						Share.ShowMessage('I0007','保存');
					}
				}
            });
		}
		// 修改保存
		else if (functionId == -1) {
			Share.AjaxRequest({
				url : ctx + '/saveInvoiceBuy',
				params : {
					invoiceBuyId : invoiceBuyId,
					invoiceBuyCode : invoiceBuyCode,
					invoiceBuySno : invoiceBuySno,
					invoiceBuyEno : invoiceBuyEno,
					invoiceBuyType : invoiceBuyType,
					invoiceBuyDate : invoiceBuyDate,
					invoiceMach : invoiceMach,
					invoiceBuyRemark : invoiceBuyRemark
				},
				callback : function(json) {
					if (json.resultcode == '0') {
						Share.ShowMessage('I0006','编辑');
						win.close();
						Ext.getCmp('invoiceSdate').setValue('');
						Ext.getCmp('invoiceEdate').setValue('');
						Ext.getCmp('invoiceBuyGridType').setValue('');
						Ext.getCmp('tab-deal-invoicebuy').getStore().reload();
					} else {
						win.close();
						Share.ShowMessage('I0007','编辑');
					}
				}
			});
		}
	},
	init : function(application) {
		this.control({
			"button" : {
				click : this.onButtonClick
			}
		});
	}
});